package com.titanrise.crm.mapper;

import com.titanrise.crm.domain.dto.RoleBelongDto;
import com.titanrise.crm.domain.model.Role;
import com.titanrise.crm.environment.annotation.*;

import java.util.List;

/**
 * @Description:
 * @Author: pront
 * @Time:2024-11-06 17:16
 */
@Mapper
public interface RoleMapper {

    @Select("select * from tb_role")
    @ResultType(Role.class)
    List<Role> selectAll();

    @Select("SELECT EXISTS(SELECT * FROM tb_role WHERE username = #{username});")
    boolean existsUsername(@Param("username") String username);

    @Update("UPDATE tb_role SET username = #{username} WHERE id = #{roleId}")
    int updateUsernameById(@Param("roleId") String roleId, @Param("username") String username);

    @Update("UPDATE tb_role SET password = #{password} WHERE id = #{roleId}")
    int updatePasswordById(@Param("roleId") String roleId, @Param("password") String password);

    @Select("select * from tb_role where username = #{username}")
    @ResultType(Role.class)
    Role selectByUsername(@Param("username") String username);

    @Select("select * from tb_role where username = #{username} and password = #{password}")
    @ResultType(Role.class)
    Role selectByUsernameAndPassword(@Param("username") String username, @Param("password") String password);

    @Insert(value =
            "INSERT INTO tb_role VALUES(#{role.id}, #{role.uid}, #{role.username}," +
                    " #{role.password}, #{role.name}, #{role.email}, #{role.role}, #{role.lockState}, #{role.createTime}, #{role.isBelong})",
            autoGenerateUUID = true)
    int insertOne(@ParamEntity("role") Role role);

    @Update("update tb_role set username = #{username}, password = #{password} where uid = #{uid}")
    int updateRoleInfoByUid(@Param("username") String username, @Param("password") String password, @Param("uid") String uid);

    @Insert("insert into tb_role" +
            "(id, uid, username, password, create_time, name, role)" +
            "value" +
            "(#{role.id}, #{role.uid}, #{role.username}, #{role.password}, #{role.createTime}, #{role.name}, #{role.role})")
    int addRole(@ParamEntity("role") Role role);

    @Delete("delete from tb_role where uid = #{uid}")
    int deleteRoleByUid(@Param("uid") String uid);

    @Select("select name from tb_role where uid = #{uid}")
    String selectNameByUid(@Param("uid") String uid);

    @ResultType(Role.class)
    @Select("select id, uid, name from tb_role where role = 1")
    List<Role> selectAllSalesperson();

    @ResultType(Role.class)
    @Select("SELECT * FROM tb_role WHERE uid = #{uid}")
    List<Role> selectCustomerByUserID(@Param("uid") String uid);

    @Select("select count(id) from tb_role where uid = #{uid}")
    int selectExistByUid(@Param("uid") String uid);

    @ResultType(RoleBelongDto.class)
    @Select("select id, uid, name from tb_role where role = 0 and is_belong = 'FALSE'")
    List<RoleBelongDto> selectNotBelongClient();

    @Update("update tb_role set is_belong = 'TRUE' where uid = #{clientUid}")
    int updateNotBelongClient(@Param("clientUid") String clientUid);

    @Update("UPDATE tb_role SET name = #{name} WHERE role_id = #{roleId}")
    int updateNameById(@Param("roleId") String roleId, @Param("name") String name);

    @Update("UPDATE tb_role SET email = #{email} WHERE id = #{roleId}")
    int updateEmailById(@Param("roleId") String roleId, @Param("email") String email);

    @Update("update tb_role set is_belong = 'FALSE' where uid = #{uid}")
    int updateClientNonBelongById(@Param("uid") String uid);

    @Select("select * from tb_role where uid = #{uid}")
    @ResultType(Role.class)
    Role selectByUId(@Param("uid") String uid);

    @Delete("delete from tb_role where uid = #{uid}")
    int deleteByUid(@Param("uid") String uid);

    @Update("update tb_role set username = #{username}, password = #{password} where uid = #{uid}")
    int updateRoleUsnaPaswByUid(@Param("uid") String uid, @Param("username") String username, @Param("password") String password);
}
